package com.flong.lambda;

import com.alibaba.fastjson.JSON;
import com.flong.vo.Student;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author:liangjl
 * @Date: 2020/6/20-12:51
 * @Eamil:jilongliang@sina.com
 * @Description:
 */
public class Test3 {

    @Test
    public void test1() {
        List<Student> students = dataList();
        //通过性别进行分组
        Map<String, List<Student>> studentGroup = students.stream().collect(Collectors.groupingBy(Student::getSex));

        for (Map.Entry<String, List<Student>> entry : studentGroup.entrySet()) {
            String key = entry.getKey();

            List<Student> studentList = entry.getValue();
            //相同的数据进行分组
            List<Student> newStus = studentList.stream().filter(stu -> stu.getSex().equals(key)).collect(Collectors.toList());
            System.out.println(JSON.toJSON(newStus));
        }

    }

    private List<Student> dataList() {
        List<Student> personList = new ArrayList<>();
        Student p1 = Student.builder().userName("周伯通").sex("男").build();
        Student p2 = Student.builder().userName("瑛姑").sex("女").build();
        Student p3 = Student.builder().userName("一灯大师").sex("男").build();
        Student p4 = Student.builder().userName("小龙女").sex("女").build();
        Student p5 = Student.builder().userName("杨过").sex("男").build();

        personList.add(p1);
        personList.add(p2);
        personList.add(p3);
        personList.add(p4);
        personList.add(p5);
        return personList;
    }
}
